package com.devops.shiro.graphql;

import com.devops.admin.model.BaseUser;
import com.devops.admin.model.TableResult;
import com.devops.admin.po.DOSBaseUserPo;
import com.devops.admin.vo.DOSPageVo;
import com.devops.common.annotation.DOSLog;
import com.devops.common.utils.PasswordUtil;
import com.devops.constant.DevOpsConstant;
import com.devops.shiro.service.DOSBaseUserService;
import com.devops.shiro.vm.*;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName DOSBaseUserDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/4 14:04
 * @Version 1.0
 **/
@DgsComponent
public class DOSBaseUserDataFetcher {

    @Autowired
    DOSBaseUserService dosBaseUserService;


    @DgsMutation
    @DOSLog(description = "添加人员")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    public String addBaseUser(@InputArgument BaseUserCreateVM userCreateVo) {
        return dosBaseUserService.addUser(userCreateVo);
    }

    @DgsMutation
    @DOSLog(description = "更新人员信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    public Boolean updateBaseUser(@InputArgument BaseUserUpdateVM userUpdateVo) {
        return dosBaseUserService.updateUser(userUpdateVo);
    }

    @DgsMutation
    @DOSLog(description = "删除人员信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    public Boolean deleteBaseUser(@InputArgument String userId) {
        return dosBaseUserService.deleteUser(userId);
    }

    /**
     * @Author heyabo
     * @Description 修改用户密码
     * @Date 15:01 2022/4/19
     * @Param [userUpdateVM]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "修改用户密码")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public Boolean updatePassword(@InputArgument DOSUserUpdatePasswordVM userUpdateVM){

        return dosBaseUserService.updatePassword(userUpdateVM);
    }

    /**
     * @Author heyabo
     * @Description 管理员修改用户密码
     * @Date 08:50 2022/4/21
     * @Param [changePassVM]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "超级管理员修改用户密码")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    public Boolean changePassword(@InputArgument DOSUserChangePasswordVM changePassVM){

        return dosBaseUserService.changePassword(changePassVM);
    }

    @DgsQuery
    @DOSLog(description = "获取所有人员信息")
    public List<BaseUser> getAllUsers() {
        return dosBaseUserService.list();
    }

    @DgsQuery
    @DOSLog(description = "分页获取人员信息")
    public TableResult<DOSBaseUserPo> getPageUsers(@InputArgument DOSUserVM dosUserVM, @InputArgument DOSPageVo inputPage) {
        return dosBaseUserService.getPageUsers(dosUserVM,inputPage);
    }


    @DgsQuery
    @DOSLog(description = "查询人员")
    public BaseUser getBaseUser() {
        return dosBaseUserService.getBaseUser();
    }

    @DgsQuery
    @DOSLog(description = "通过名称获取人员信息")
    public List<BaseUser> getUsersByName(String name) {
        return dosBaseUserService.getUsersByName(name);
    }

    /**
     * @Author heyabo
     * @Description 获取待分配的技术经理
     * @Date 17:01 2022/9/5
     * @Param []
     * @return java.util.List<com.devops.admin.model.BaseUser>
     **/
    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public List<BaseUser> getTechnologyManager(){

        return dosBaseUserService.getTechnologyManager();
    }

    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR},logical = Logical.OR)
    public String generateUserPassword(){
        return PasswordUtil.generatePassword();
    }
}
